package com.shujia.array;

import org.junit.Test;

import java.util.Arrays;

public class Homework4 {

    @Test
    public void test1() {
        /*
        数组A：1，7，9，11，13，15，17，19；数组b：2，4，6，8，10
        两个数组合并为数组c，按升序排列
         */
        int[] arrA = {1, 7, 9, 11, 13, 15, 17, 19};
        int[] arrB = {2, 4, 6, 8, 10};
        int[] target = new int[arrA.length + arrB.length];
        for (int i = 0; i < target.length; i++) {
            if (i < arrA.length) {
                target[i] = arrA[i];
            } else {
                target[i] = arrB[i - arrA.length];
            }
        }
        System.out.println(Arrays.toString(target));
        Arrays.sort(target);// 升序
        System.out.println(Arrays.toString(target));

    }

    public static void main(String[] args) {
        int[] arr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
        int[] newArr = mergeArray(arr);
        System.out.println("merge:" + Arrays.toString(newArr));

    }

    // 排序的是数组
    public static int[] mergeArray(int[] arr) {
        int count = 0;
        for (int num : arr) {
            if (num == 0) {
                continue;
            }
            count++;
        }
        int[] newArr = new int[count];
        int index = 0;
        for (int num : arr) {
            if (num == 0) {
                continue;
            } else {
                newArr[index++] = num;
            }
        }

        return newArr;
    }
}
